const pickMax = (nums, k) => {
    let stack = [];
    let drop = nums.length - k;
    // 3465 912583
    for (let i = 0; i < nums.length; i++) {
        while (stack.length && stack[stack.length - 1] < nums[i] && drop) {
            stack.pop();
            drop--;
        }
        stack.push(nums[i]);
    }
    while (drop--) {
        stack.pop();
    }
    return stack;
};

const merge = (A, B) => {
    let ans = [];
    while (A.length || B.length) {
        let bigger = A > B ? A : B;
        ans.push(bigger.shift());
    }
    return ans;
};

const maxNumber = (nums1, nums2, k) => {
    let ans = [];
    for (let i = 0; i <= k; i++) {
        if (i <= nums1.length && k - i <= nums2.length) {
            console.log(pickMax(nums1, i), pickMax(nums2, k - i));
            ans = ans > merge(pickMax(nums1, i), pickMax(nums2, k - i)) ? ans : merge(pickMax(nums1, i), pickMax(nums2, k - i));
        }
    }
    return ans;
};
console.log(maxNumber([3, 4, 6, 5], [9, 1, 2, 5, 8, 3], 5));